package com.newcoder.community.service;

import com.newcoder.community.dao.AlphaDao;
import com.newcoder.community.dao.DiscussPostMapper;
import com.newcoder.community.dao.UserMapper;
import com.newcoder.community.entity.DiscussPost;
import com.newcoder.community.entity.User;
import com.newcoder.community.util.CommunityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.Date;

@Service
//@Scope("prototype")//实际中很少
public class AlphaService {

    @Autowired
    private AlphaDao alphaDao;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private DiscussPostMapper discussPostMapper;

    //注入管理事务的bean(spring自动创建装配)
    @Autowired
    private TransactionTemplate transactionTemplate;


    public AlphaService() {
//        System.out.println("实例化AlphaService");
    }

    @PostConstruct//在构造器之后调用
    public void init() {
//        System.out.println("初始化AlphaService");
    }

    @PreDestroy//在销毁对象之前调用
    public void destory() {
//        System.out.println("销毁AlphaService");
    }

    public String find() {
       return alphaDao.select();
    }
    // ==============================

    // 事务的传播机制(概述)
    // REQUIRED: 支持当前事务(外部事务)，如果不存在则创建新事务。
    // REQUIRES_NEW: 创建一个新事务，并暂停当前事务(外部事务)。
    // NESTED: 如果当前存在事务(外部事务)，则嵌套在该事务中执行(独立的提交和回滚)，否则(外部事务不存在)就会REQUIRED一样。
    // 通过注解管理事务
    @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED)
    public Object save1() {
        // 新增用户
        User user = new User();
        user.setUsername("龙宝");
        user.setSalt(CommunityUtil.generateUUID().substring(0,5));
        user.setPassword(CommunityUtil.md5("123" + user.getSalt()));
        user.setEmail("longbao@qq.com");
        user.setHeaderUrl("http://iamge.nowcoder.com/head/99t.png");
        user.setCreateTime(new Date());
        userMapper.insertUser(user);

        // 新增帖子
        DiscussPost discussPost = new DiscussPost();
        discussPost.setUserId(user.getId());
        discussPost.setTitle("龙宝sb");
        discussPost.setContent(" 阿伟sb");
        discussPost.setCreateTime(new Date());
        discussPostMapper.insertDiscussPost(discussPost);

        Integer.valueOf("aaa");// 错误点

        return "ok";
    }

    // 编程式事务
    public Object save2() {
        // 事务隔离级别
        transactionTemplate.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);
        // 事务的传播机制
        transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);

        return transactionTemplate.execute(new TransactionCallback<Object>() {
            @Override
            public Object doInTransaction(TransactionStatus transactionStatus) {
                // 新增用户
                User user = new User();
                user.setUsername("阿伟");
                user.setSalt(CommunityUtil.generateUUID().substring(0,5));
                user.setPassword(CommunityUtil.md5("123" + user.getSalt()));
                user.setEmail("awei@qq.com");
                user.setHeaderUrl("http://iamge.nowcoder.com/head/929t.png");
                user.setCreateTime(new Date());
                userMapper.insertUser(user);

                // 新增帖子
                DiscussPost discussPost = new DiscussPost();
                discussPost.setUserId(user.getId());
                discussPost.setTitle("龙宝asb");
                discussPost.setContent(" 阿伟asb");
                discussPost.setCreateTime(new Date());
                discussPostMapper.insertDiscussPost(discussPost);

                Integer.valueOf("asa");// 错误点

                return "ok";
            }
        });

    }



}
